#coding=utf-8
import os
import os.path as path


class config(object):
    port = 0
    gb2utf8 = False
    use_websever_script_name = False
    script_name = ''


def get(sitepath):
    conffile = path.join(sitepath,'scgi.conf')
    if path.exists(sitepath):
        conf = open(conffile,'r')
    else:
        raise Exception('Can not find file "scgi.conf"')
    settings = {}
    for line in conf.readlines():
        line = line.strip()
        if line.startswith('#'):
            continue
        kv = line.split('=',1)
        if len(kv) < 2:
            continue
        settings[kv[0].strip()] = kv[1].strip();
    try:
        assert 'port' in settings
        c = config()
        c.port = int(settings['port'])
        c.gb2utf8 = eval(settings.get('url_gb2312_to_utf-8','False'))
        c.script_name = settings.get('script_name','')
        c.use_webserver_script_name = eval(settings.get('use_webserver_script_name','False'))
    except:
        raise Exception('Can not find "%s" in scgi.conf' % (k,))
    return c
